#!/usr/bin/python
# -*- coding: UTF-8 -*-
from model import anagraphdata
from model import anagraphdistance
import storagedistance
import sys
import multiprocessing
import time
import random

reload(sys)
sys.setdefaultencoding('utf8')

def initData():
    obj_ana = anagraphdata.AnagraphData()
    ana_list = obj_ana.getAnagraphs('伤寒杂病论', ['name', 'medicines'])

    # 添加金匮数据
    append_jk_list = []
    append_jk_list = obj_ana.getAnagraphs('金匮要略', ['name', 'medicines'])
    ana_list.extend(append_jk_list)

    # 添加肘后数据
    append_zh_list = []
    append_zh_list = obj_ana.getAnagraphs('肘后备急方', ['name', 'medicines'])
    ana_list.extend(append_zh_list)

    # 添加肘后数据
    # append_bj_list = []
    # append_bj_list = obj_ana.getAnagraphs('备急千金要方', ['name', 'medicines'])
    # ana_list.extend(append_bj_list)

    return ana_list

def mapSetDistance(process_index, node_list, min_index, max_index):
    # 先计算好所有的距离，并存入mongoDB
    obj_store = storagedistance.StorageDistance()
    obj_store.setNodeList(node_list)
    obj_store.setDistanceAlgorithm(algorithm_type = 3)

    print "::process ID: ", process_index
    obj_store.storeDistance(min_index, max_index)
    print "::process %d end" %(process_index)

if __name__ == "__main__":
    print "cpu counts: " + str(multiprocessing.cpu_count())

    node_list       = initData()
    node_count      = len(node_list)
    step            = 50
    ranges          = list(range(node_count))
    ranges          = ranges[0:node_count:step]

    print "~~~~~~~~Begin Multiprocessing~~~~~~~~\n"
    pool = multiprocessing.Pool(processes = 10)

    process_index = 0
    for min_index in ranges:
        max_index = min_index + step - 1
        pool.apply(mapSetDistance, (process_index, node_list, min_index, max_index,))
        process_index += 1

    pool.close()
    pool.join()   #调用join之前，先调用close函数，否则会出错。执行完close后不会有新的进程加入到pool,join函数等待所有子进程结束

    print "Sub-processes done."

# # 添加金匮数据
# append_list = []
# append_list = obj_ana.getAnagraphs('金匮要略')
#
# obj_ndcluster.appendNodes(append_list)
# append_dict = obj_ndcluster.appendCluster()
# node_list = obj_ndcluster.getNodes()
#
# drawGraph(node_list, ana_dict, 'Jacarrd-伤寒-金匮-0.6')
#
# # 添加肘后数据到图中
# append_list = []
# # 获取指定方剂来源
# append_list = obj_ana.getAnagraphs('肘后备急方')
# # 添加数据
# obj_ndcluster.appendNodes(append_list)
# # 进行追加聚类
# append_dict = obj_ndcluster.appendCluster()
# # 获取所有节点
# node_list = obj_ndcluster.getNodes()
# # 指定输出文件名称
# drawGraph(node_list, ana_dict, 'Jacarrd-伤寒-金匮-肘后-0.6')
#
# # 添加肘后数据到图中
# append_list = []
# # 获取指定方剂来源
# append_list = obj_ana.getAnagraphs('备急千金要方')
# # 添加数据
# obj_ndcluster.appendNodes(append_list)
# # 进行追加聚类
# append_dict = obj_ndcluster.appendCluster()
# # 获取所有节点
# node_list = obj_ndcluster.getNodes()
# # 指定输出文件名称
# drawGraph(node_list, ana_dict, 'Jacarrd-伤寒-金匮-肘后-千金-0.6')

# # 添加肘后数据到图中
# append_list = []
# # 获取指定方剂来源
# append_list = obj_ana.getAnagraphs('千金翼方')
# # 添加数据
# obj_ndcluster.appendNodes(append_list)
# # 进行追加聚类
# append_dict = obj_ndcluster.appendCluster()
# # 获取所有节点
# node_list = obj_ndcluster.getNodes()
# # 指定输出文件名称
# drawGraph(node_list, ana_dict, 'Jacarrd-伤寒-金匮-肘后-千金-千金翼-0.6')
